package com.estronger.shopmanage.ui.personal;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingyg.updateutil.CustomDialog;
import com.dingyg.updateutil.IntranetUpdateManager;
import com.dingyg.updateutil.UpdateApp;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.utils.ActivityUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.Toast;
import com.soonfor.evaluate.http.httptools.NetUtils;
import com.tbruyelle.rxpermissions2.Permission;

public class AboutUsActivity extends Activity {

    private RelativeLayout rlBack;
	private TextView tv_ver_name;
	private TextView tv_have_new;
	private RelativeLayout rlfCheckVersion;//检测版本
	private RelativeLayout rlfAppBarCode;//应用二维码
	//官网更新
	private IntranetUpdateManager intUpdateManager;
	private boolean isClickUpdate = false;//是否点击更新按钮进行更新

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_about_us);
        rlBack = findViewById(R.id.rl_back);
		tv_ver_name = (TextView) findViewById(R.id.tv_ver_name);
		tv_have_new = findViewById(R.id.tv_have_new);
        rlfCheckVersion = findViewById(R.id.rlfCheckVersion);
        rlfAppBarCode = findViewById(R.id.llfAppBarCode);

        // 点击退出界面
        rlBack.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
		getVersionName();
		rlfCheckVersion.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				isClickUpdate = true;
				try {
					PermissionUtils.getInstance().requestPromisssions(AboutUsActivity.this, new PermissionUtils.AcceptListener() {
								@Override
								public void grante() {
									if (!NetUtils.isConnectInternet(AboutUsActivity.this)) {
										Toast.show(AboutUsActivity.this, "当前无网络，请开启网络后再更新！", 2000);
										return;
									}
									try {
										new Thread(checkSelfUpdateInt).start();
									} catch (Exception e) {
									}
								}

								@Override
								public void denial(Permission permission) {
								}

								@Override
								public void fail() {

								}
							},
							Manifest.permission.READ_EXTERNAL_STORAGE,
							Manifest.permission.WRITE_EXTERNAL_STORAGE,
							Manifest.permission.READ_PHONE_STATE);

				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		rlfAppBarCode.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

			}
		});
		//官网更新
		String url_b = "http://www.soonfor.com/appdownload/homecase/apk/";
		String url[] = new String[]{url_b + "version.txt", url_b};
		intUpdateManager = new IntranetUpdateManager(AboutUsActivity.this, url);
		isClickUpdate = false;
		try {
			new Thread(checkSelfUpdateInt).start();
		} catch (Exception e) {
		}
}

	private void getVersionName() {
		// 获取包管理者
		PackageManager pm = this.getPackageManager();
		try {
			// 获取包信息
			PackageInfo packageInfo = pm.getPackageInfo(this.getPackageName(),
					0);
			// 获取versionName
			tv_ver_name.setText("V" + packageInfo.versionName);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	Dialog updateDialog;
	//内网更新
	Runnable checkSelfUpdateInt = new Runnable() {
		@Override
		public void run() {
			// TODO 这里写上传逻辑
			try {
				int localVerCode = intUpdateManager.getLocalVerCode();
				int serverVerCode = intUpdateManager.getServerVerCode();
				if(isClickUpdate) {
					// 如果服务器版本高于本地版本则提示更新
					if (serverVerCode > localVerCode) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								if (ActivityUtils.isRunning(AboutUsActivity.this)) {
									if (System.currentTimeMillis() - UpdateApp.lastUpdateTime > 0) {
										UpdateApp.lastUpdateTime = System.currentTimeMillis();
										updateDialog = CustomDialog.createUpdateDialog(AboutUsActivity.this,
												new View.OnClickListener() {
													@Override
													public void onClick(View v) {
														updateDialog.dismiss();
														updateDialog.setCancelable(true);
														intUpdateManager.checkIsAndroid0();
													}
												}, null);
										updateDialog.show();
									}
								}
							}
						});
					} else {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								Toast.show(AboutUsActivity.this, "当前已是最新版本", Toast.LENGTH_SHORT);
							}
						});
					}
				}else {
					if (serverVerCode > localVerCode) {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								tv_have_new.setVisibility(View.VISIBLE);
							}
						});
					}else {
						runOnUiThread(new Runnable() {
							@Override
							public void run() {
								tv_have_new.setVisibility(View.GONE);
							}
						});
					}
				}
			} catch (Exception e) {
			}
		}
	};

}
